package com.drico.grafo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.drico.exceptions.NotFoudEdgeException;
import com.drico.interfaces.Visitable;

public class Vertex implements Comparable<Vertex>, Visitable {

	private String name;

	private List<Edge> adjacencies = new ArrayList<Edge>();
	private ArrayList<String> adjacenciesName = new ArrayList<String>();

	private double minDistance = Double.POSITIVE_INFINITY;
	private Vertex previous;

	public Vertex(String argName) {
		name = argName;
	}

	public String toString() {
		return name;
	}

	public int compareTo(Vertex other) {
		return Double.compare(minDistance, other.minDistance);
	}

	public double hasEdge(String adj) throws NotFoudEdgeException {
		double weight = 0;
		if (!adjacenciesName.contains(adj)) {
			throw new NotFoudEdgeException("SIN RUTA");
		}

		Iterator<Edge> edgeIt = adjacencies.iterator();
		while (edgeIt.hasNext()) {
			Edge edge = edgeIt.next();
			if (edge.getTarget().getName().equals(adj))
				weight = edge.getWeight();
		}
		return weight;
	}

	public void clean() {
		this.minDistance = Double.POSITIVE_INFINITY;
		this.previous = null;
	}

	public List<Edge> getAdjacencies() {
		return adjacencies;
	}

	public void addAdjacencies(Edge adjacenci) {
		this.adjacenciesName.add(adjacenci.getTarget().getName());
		this.adjacencies.add(adjacenci);
	}

	public ArrayList<String> getAdjacenciesName() {
		return adjacenciesName;
	}

	public double getMinDistance() {
		return minDistance;
	}

	public void setMinDistance(double minDistance) {
		this.minDistance = minDistance;
	}

	public Vertex getPrevious() {
		return previous;
	}

	public void setPrevious(Vertex previous) {
		this.previous = previous;
	}

	public String getName() {
		return name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vertex other = (Vertex) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public boolean isVisitable(Vertex vAdj) {
		return this.adjacenciesName.contains(vAdj.getName());
	}

}
